package com.cskaoyan.service;

import com.cskaoyan.bean.Keyword;
import com.cskaoyan.bean.KeywordExample;
import com.cskaoyan.mapper.KeywordMapper;
import com.cskaoyan.vo.KeywordVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;

/**
 * @ description:
 * @ author:台
 **/

@Service
public class KeywordServiceImpl implements KeywordService {

    @Autowired
    KeywordMapper keywordMapper;

    @Override
    public KeywordVo keywordList(Integer page, Integer limit, String sort, String desc) {
        PageHelper.startPage(page, limit);

        KeywordExample keywordExample = new KeywordExample();
        keywordExample.setOrderByClause(sort + " " + desc);
        keywordExample.createCriteria().andDeletedEqualTo(false);
        List<Keyword> keywords = keywordMapper.selectByExample(keywordExample);

        PageInfo<Keyword> keywordPageInfo = new PageInfo<>(keywords);
        long total = keywordPageInfo.getTotal();

        KeywordVo keywordVo = new KeywordVo();
        keywordVo.setItems(keywords);
        keywordVo.setTotal(total);
        return keywordVo;
    }

    @Override
    public Keyword keywordUpdate(Keyword keyword) {
        int i = keywordMapper.updateByPrimaryKey(keyword);
        Keyword keyword1 = keywordMapper.selectByPrimaryKey(keyword.getId());
        return keyword1;
    }

    @Override
    public Keyword keywordCreate(Keyword keyword) {
        int insert = keywordMapper.insertSelective(keyword);
        Integer id = keyword.getId();
        Keyword data = keywordMapper.selectByPrimaryKey(id);
        return data;
    }

    @Override
    public void keywordDelete(Keyword keyword) {
        KeywordExample keywordExample = new KeywordExample();
        KeywordExample.Criteria criteria = keywordExample.createCriteria();
        criteria.andIdEqualTo(keyword.getId());
        Keyword keyword1 = new Keyword();
        keyword1.setDeleted(true);
        keywordMapper.updateByExampleSelective(keyword1,keywordExample);
    }
}
